"use strict"
var call = require("../../utils/request.js")

const app = getApp()
Page({
  data: {
    items: [
      { name: 'shizu', value: '时租房',checked: 'true'  },
      { name: 'quanri', value: '全日房', },
    ],
    radioStr: '中国',
    hidden1:true,
    hidden2:false,
    ffshow:false,
    multiArray: [['今天', '明天', '3-2', '3-3', '3-4', '3-5'], [0, 1, 2, 3, 4, 5, 6], [0, 10, 20]],
    multiIndex: [0, 0, 0],
    indicatorDots: true,
    interval: 2000,
    duration: 500,
    time: "请选择入住时间",
    navLists: ['费用明细', '设施与服务', '入住须知', '房间介绍'],
    tabId: 'tab_0',
    isFixedNav: false,
    scrollTop: 0,
    viewHeight: 0,
    toView: '',
    detail: [],
    servicelist: [],
    starttime: '',
    endtime: '',
    touchS : [0,0],
    touchE : [0,0],
    ruzhutype:'shizu',
    ws:'5',
    hj:'5',
    fw:'5',
    ss:'5',
    iffe:true,
    remark:[
     
    ]

  },

  onLoad: function (e) {


    wx.setNavigationBarTitle({
      title: '房间详情'
    })

    //  e.roomid=77;

    var url = 'getRdetail?id=' + e.roomid;
    call.getData(url, this.getRdetail, this.fail)


    var url2 = 'getcommed?roomid=' + e.roomid;
    call.getData(url2, this.getcommed, this.fail)

    var url3 = 'getdafen?roomid=' + e.roomid;
    call.getData(url3, this.getdafen, this.fail)


    let systemInfo = wx.getSystemInfoSync()
    let screenWidth = systemInfo.screenWidth
    this.swiperHeight = screenWidth * 8 / 15
    this.setData({
      roomid: e.roomid,
      viewHeight: systemInfo.windowHeight - 50
    })
  },
  touchend: function (e) {
    let start = this.data.touchS
    let end = this.data.touchE
    console.log(start)
    console.log(end)
    if(start[0] < end[0] - 50){
      console.log('右滑')
    }else if(start[0] > end[0] + 50){
      console.log('左滑')
      wx.navigateBack({
        delta: 1  // 返回上一级页面。
      })
    }else{
      console.log('静止')
    }

	},
  
  getdafen: function (data) {
    console.log(data)
    if(data == 111){
    
    }else{
      this.setData({
        ws:data.data.ws,
        hj:data.data.hj,
        fw:data.data.fw,
        ss:data.data.ss,
      })
    }

  },
  getcommed: function (data) {
      console.log(data)
      if(data == 111){
        this.setData({
          iffe:true,
        })
      }else{
        this.setData({
          remark:data.data,
          iffe:false,
        })
      }
    
  },
  pickerTap: function () {
    console.log(1111)

    var date = new Date();
   

    var monthDay = ['今天', '明天'];
    var hours = [];
    var minute = [];

    var currentHours = date.getHours();
    var currentMinute = date.getMinutes();

    // 月-日
    for (var i = 2; i <= 28; i++) {
      var date1 = new Date(date);
      date1.setDate(date.getDate() + i);
      var md = (date1.getMonth() + 1) + "-" + date1.getDate();
      monthDay.push(md);
    }
    // this.setData({
    //   starttime:'',
    //   endtime:'',
    // })
    var data = {
      multiArray: this.data.multiArray,
      multiIndex: [0, 0, 0],
    };

    if (data.multiIndex[0] === 0) {
      if (data.multiIndex[1] === 0) {
        this.loadData(hours, minute, currentHours, currentMinute);
      } else {
        this.loadMinute(hours, minute);
      }
    } else {
      this.loadHoursMinute(hours, minute);
    }

    data.multiArray[0] = monthDay;
    data.multiArray[1] = hours;
    data.multiArray[2] = minute;

    this.setData(data);
  },

  radioChange: function (e) {
    this.setData({
      starttime:'',
      endtime:'',
    })
    var that = this
      console.log(e.detail.value)
    if(e.detail.value == 'shizu'){
      console.log("shizu")
      this.setData({
        hidden1:true,
        hidden2:false,
        ffshow:false,
        ruzhutype:'shizu'
      })
    }else{
      console.log("quanri")
      this.setData({
        hidden1:false,
        hidden2:true,
        ffshow:true,
        ruzhutype:'quanri'
      })
    }
  },



  gohome: function (data) {
    wx.reLaunch({
      url: '../index/index',
    })
  },

  bindMultiPickerColumnChange: function (e) {
    console.log(9999)
    var date = new Date();

    var that = this;

    var monthDay = ['今天', '明天'];
    var hours = [];
    var minute = [];

    var currentHours = date.getHours();
    var currentMinute = date.getMinutes();

    var data = {
      multiArray: this.data.multiArray,
      multiIndex: this.data.multiIndex
    };
    // 把选择的对应值赋值给 multiIndex
    data.multiIndex[e.detail.column] = e.detail.value;

    // 然后再判断当前改变的是哪一列,如果是第1列改变
    if (e.detail.column === 0) {
      // 如果第一列滚动到第一行
      if (e.detail.value === 0) {

        that.loadData(hours, minute, currentHours, currentMinute);

      } else {
        that.loadHoursMinute(hours, minute);
      }

      data.multiIndex[1] = 0;
      data.multiIndex[2] = 0;

      // 如果是第2列改变
    } else if (e.detail.column === 1) {
      

      // 如果第一列为今天
      if (data.multiIndex[0] === 0) {

        if (e.detail.value === 0) {
          that.loadData(hours, minute, currentHours, currentMinute);
        } else {
  
          that.loadMinute(hours, minute);
        }
        // 第一列不为今天
      } else {
        that.loadHoursMinute(hours, minute);
      }
      data.multiIndex[2] = 0;

      // 如果是第3列改变
    } else {
      // 如果第一列为'今天'
      if (data.multiIndex[0] === 0) {

        // 如果第一列为 '今天'并且第二列为当前时间
        if (data.multiIndex[1] === 0) {
          that.loadData(hours, minute, currentHours, currentMinute);
        } else {
          that.loadMinute(hours, minute);
        }
      } else {
        that.loadHoursMinute(hours, minute);
      }
    }
    data.multiArray[1] = hours;
    data.multiArray[2] = minute;
    this.setData(data);
  },

  loadData: function (hours, minute, currentHours, currentMinute) {

    var minuteIndex;
    if (currentMinute > 0 && currentMinute <= 10) {
      minuteIndex = 10;
    } else if (currentMinute > 10 && currentMinute <= 20) {
      minuteIndex = 20;
    } else if (currentMinute > 20 && currentMinute <= 30) {
      minuteIndex = 30;
    } else if (currentMinute > 30 && currentMinute <= 40) {
      minuteIndex = 40;
    } else if (currentMinute > 40 && currentMinute <= 50) {
      minuteIndex = 50;
    } else {
      minuteIndex = 60;
    }
    //wmc
    // var minuteIndex = 0
    // var currentHours=0
    if (minuteIndex == 60) {
      // 时
      for (var i = currentHours + 1; i < 24; i++) {
        hours.push(i);
      }
      // 分
      for (var i = 0; i < 60; i += 10) {
        minute.push(i);
      }
    } else {
      // 时
      for (var i = currentHours; i < 24; i++) {
        hours.push(i);
      }
      // 分


      for (var i = minuteIndex; i < 60; i += 10) {
        minute.push(i);
      }
    }
  },

  loadHoursMinute: function (hours, minute) {
    // 时
    for (var i = 0; i < 24; i++) {
      hours.push(i);
    }
    // 分
    for (var i = 0; i < 60; i += 10) {
      minute.push(i);
    }
  },

  loadMinute: function (hours, minute) {


    var minuteIndex;

    var date = new Date();
   
      var currentMinute = date.getMinutes();
      var currentHours = date.getHours();

    if (currentMinute > 0 && currentMinute <= 10) {
      minuteIndex = 10;
    } else if (currentMinute > 10 && currentMinute <= 20) {
      minuteIndex = 20;
    } else if (currentMinute > 20 && currentMinute <= 30) {
      minuteIndex = 30;
    } else if (currentMinute > 30 && currentMinute <= 40) {
      minuteIndex = 40;
    } else if (currentMinute > 40 && currentMinute <= 50) {
      minuteIndex = 50;
    } else {
      minuteIndex = 60;
    }

    var minuteIndex = 0;
    if (minuteIndex == 60) {
      // 时
      for (var i = currentHours + 1; i < 24; i++) {
        hours.push(i);
      }
    } else {
      // 时
      for (var i = currentHours; i < 24; i++) {
        hours.push(i);
      }
    }
    // 分
    for (var i = 0; i < 60; i += 10) {
      minute.push(i);
    }
  },

  bindStartMultiPickerChange: function (e) {
    console.log(3433)
    var that = this;
    var monthDay = that.data.multiArray[0][e.detail.value[0]];
    var hours = that.data.multiArray[1][e.detail.value[1]];
    var minute = that.data.multiArray[2][e.detail.value[2]];
    var date = new Date();
    if (monthDay === "今天") {
      var month = date.getMonth() + 1;
      var day = date.getDate();
      monthDay = month + "-" + day + "";
    } else if (monthDay === "明天") {
      var date1 = new Date(date);
      date1.setDate(date.getDate() + 1);
      monthDay = (date1.getMonth() + 1) + "-" + date1.getDate() + "";

    } else {
      var month = monthDay.split("-")[0]; // 返回月
      var day = monthDay.split("-")[1]; // 返回日
      if (month < 10) {
        console.log('月小于10')
        var month = '0' + month
      }
      if (day < 10) {
        console.log('日小于10')
        var day = '0' + day
      }
      monthDay = month + "-" + day + "";
    }
    if (hours < 10) {
      var hours = '0' + hours
    }

    if (minute < 10) {
      var minute = '0' + minute
    }


    var starttime = monthDay + " " + hours + ":" + minute;
    that.setData({
      starttime: starttime
    })
  },


  bindStartMultiPickerChange2: function (e) {
    var that = this;
    var monthDay = that.data.multiArray[0][e.detail.value[0]];
    var hours = that.data.multiArray[1][e.detail.value[1]];
    var minute = that.data.multiArray[2][e.detail.value[2]];
    var date = new Date();
    if (monthDay === "今天") {
      var month = date.getMonth() + 1;
      var day = date.getDate();
      monthDay = month + "-" + day + "";
    } else if (monthDay === "明天") {
 
      var date1 = new Date(date);
      date1.setDate(date.getDate() + 1);
      monthDay = (date1.getMonth() + 1) + "-" + date1.getDate() + "";

    } else {
      var month = monthDay.split("-")[0]; // 返回月
      var day = monthDay.split("-")[1]; // 返回日
      if (month < 10) {
        console.log('月小于10')
        var month = '0' + month
      }
      if (day < 10) {
        console.log('日小于10')
        var day = '0' + day
      }
      monthDay = month + "-" + day + "";
    }
    if (hours < 10) {
      var hours = '0' + hours
    }

    if (minute < 10) {
      var minute = '0' + minute
    }


    var endtime = monthDay + " " + hours + ":" + minute;
    that.setData({
      endtime: endtime
    })
  },

  getRdetail: function (e) {

    this.setData({
      detail: e.data,

    })


  },

  starttime: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      starttime: e.detail.value
    })
  },

  endtime: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      endtime: e.detail.value
    })
  },
  toOrder: function (e) {
    var that = this
    console.log(that.data.starttime)
    //先判断用户有无选择时间
    if(this.data.hidden1 == true){
      if (!this.data.starttime) {
        wx.showToast({
          title: '请先选择入住时间',
          icon: 'none',
          duration: 2000,
          mask: true
        })
      } else {
        console.log(this.data.starttime)
        console.log(this.data.endtime)
        if (wx.getStorageSync("uid")) {
          console.log('makeorder')
          console.log(wx.getStorageSync("uid"))
          if (this.data.endtime) {
  
            var url = 'makeOrder?id=' + this.data.roomid + '&uid=' + wx.getStorageSync("uid") + '&stime=' + this.data.starttime + '&etime=' + this.data.endtime;
          } else {
            console.log(2)
            var url = 'makeOrder?id=' + this.data.roomid + '&uid=' + wx.getStorageSync("uid") + '&stime=' + this.data.starttime;
          }
  
          call.getData(url, this.makeOrder, this.fail)
        } else {
          wx.showModal({
            title: '提示',
            content: '您尚未登录,是否立即登录？',
            showCancel: true,//是否显示取消按钮
            cancelText: "取消",//默认是“取消”
            cancelColor: '#333',//取消文字的颜色
            confirmText: "登录",//默认是“确定”
            confirmColor: '#333',//确定文字的颜色
            success: function (res) {
              wx.redirectTo({
                url: '../hoteldetail/hoteldetail',
              })
            },
          })
        }
  
      }
    }

    if(this.data.hidden2 == true){
      if (!this.data.starttime) {
        wx.showToast({
          title: '请先选择开始时间',
          icon: 'none',
          duration: 2000,
          mask: true
        })
      } else if(!this.data.endtime){
        wx.showToast({
          title: '请先选择结束时间',
          icon: 'none',
          duration: 2000,
          mask: true
        })
      }else {
        console.log(this.data.starttime)
        console.log(this.data.endtime)
        if (wx.getStorageSync("uid")) {
          console.log('makeorder')
          console.log(wx.getStorageSync("uid"))
          if (this.data.endtime) {
  
            var url = 'makeOrder?id=' + this.data.roomid + '&uid=' + wx.getStorageSync("uid") + '&stime=' + this.data.starttime + '&etime=' + this.data.endtime;
          } else {
            console.log(2)
            var url = 'makeOrder?id=' + this.data.roomid + '&uid=' + wx.getStorageSync("uid") + '&stime=' + this.data.starttime;
          }
  
          call.getData(url, this.makeOrder, this.fail)
        } else {
          wx.showModal({
            title: '提示',
            content: '您尚未登录,是否立即登录？',
            showCancel: true,//是否显示取消按钮
            cancelText: "取消",//默认是“取消”
            cancelColor: '#333',//取消文字的颜色
            confirmText: "登录",//默认是“确定”
            confirmColor: '#333',//确定文字的颜色
            success: function (res) {
              wx.redirectTo({
                url: '../hoteldetail/hoteldetail',
              })
            },
          })
        }
  
      }
    }
   

  },
  makeOrder: function (e) {
    if (e.code == 1) {
      wx.showToast({
        title: '房间已成功为您预定,请及时支付',
        icon: 'none',
        duration: 3000,
        mask: true
      })

      wx.navigateTo({
        url: '../order/order?orderid=' + e.data +'&ruzhutype='+this.data.ruzhutype,
      })

    } else if (e.code == 5) {
      //下了这个订单没支付 跳订单列表
      wx.showToast({
        title: e.msg,
        icon: 'none',
        duration: 3000,
        mask: true
      })
      wx.switchTab({
        url: '../order_list/order_list',
      })
    }
    else {
      //有问题的情况下 报错
      wx.showToast({
        title: e.msg,
        icon: 'none',
        duration: 3000,
        mask: true
      })
    }

  },

  changeNav: function (e) {
    this.tabId = e.currentTarget.id
    this.setData({
      tabId: this.tabId,
      toView: this.tabId
    })
  },

  pageScroll: function (e) {
    let scrollTop = e.detail.scrollTop
    let isFixedNav = scrollTop > this.swiperHeight ? true : false
    this.setData({
      isFixedNav
    })
  },

})